package main

import (
	"fmt"
	"sort"
)

func main() {
	hp := InitNewHeap([]int{2, 31, 45, 14, 1, 1, 24, 12, 56, 6, 5, 4, 6}, true)
	hp.Push(3)
	println(hp.Len())
	hp.Pop()
	println(hp.Len())
	hp.Sort()
	println(hp.Less(5, 6))
	fmt.Printf("%#v", hp.IntSlice)

}

type heap struct {
	sort.IntSlice
	bool
}

/**
 *	initNewHeap
 *	@Description:
 *	@param intSlice []int
 *	@param maxOrMin bool true 大根堆 false 小根堆
 *	@return *heap
 */
func InitNewHeap(intSlice []int, maxOrMin bool) *heap {
	if intSlice == nil {
		intSlice = make([]int, 0)
	}
	return &heap{
		intSlice,
		maxOrMin,
	}
}

func (hp *heap) Pop() (v interface{}) {
	if hp.bool {
		n := hp.IntSlice.Len()
		v = hp.IntSlice[n-1]
		hp.IntSlice = hp.IntSlice[:n-1]
	} else {
		v = hp.IntSlice[0]
		hp.IntSlice = hp.IntSlice[1:]
	}
	return
}

func (hp *heap) Push(x interface{}) {
	hp.IntSlice = append(hp.IntSlice, x.(int))
}

func (hp heap) Sort() {
	sort.Slice(hp.IntSlice, func(i, j int) bool {
		if hp.bool {
			return hp.IntSlice[i] > hp.IntSlice[j]
		} else {
			return hp.IntSlice[i] < hp.IntSlice[j]
		}
	})
}
func (hp heap) Less(i, j int) bool {
	if hp.bool {
		return hp.IntSlice[i] > hp.IntSlice[j]
	} else {
		return hp.IntSlice[i] < hp.IntSlice[j]
	}
}
